package org.jeecg.modules.c.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.c.entity.DSignalH;
import org.jeecg.modules.c.entity.MSignal;
import org.jeecg.modules.c.mapper.DSignalHMapper;
import org.jeecg.modules.c.service.IDSignalHService;
import org.jeecg.modules.c.service.IMSignalService;
import org.jeecg.modules.concat.FocConcat;
import org.jeecg.modules.dto.PushAiDiDTO;
import org.jeecg.modules.foc.entity.FocPointData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 历史数据管理
 * @Author: jeecg-boot
 * @Date: 2024-11-05
 * @Version: V1.0
 */
@Service
@Slf4j
public class DSignalHServiceImpl extends ServiceImpl<DSignalHMapper, DSignalH> implements IDSignalHService {


    @Autowired
    private DSignalHMapper dSignalHMapper;
    @Autowired
    private IMSignalService mSignalService;

    @Override
    public List<DSignalH> getListLike(String id) {
        return null;
    }

    /**
     * 获取信号对对应的数据
     *
     * @param totalSignalHList
     * @return
     */
    @Override
    public List<PushAiDiDTO> dSignalHService(List<MSignal> totalSignalHList) {
        List<PushAiDiDTO> result = dSignalHMapper.dSignalHService(totalSignalHList);

        return result;
    }

    @Override
    public List<DSignalH> getSendData() {
        return dSignalHMapper.getSendData();

    }

    /**
     * 实时保存历史数据
     *
     * @param focPointData
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onlineSave(FocPointData focPointData) {
       // log.info("开始映射数据:"+focPointData.getPortGuid());
        //判断信号是否经过映射
        QueryWrapper<MSignal> queryWrapper = new QueryWrapper();
        queryWrapper.eq("guid", focPointData.getPortGuid());
        MSignal mSignal = mSignalService.getOne(queryWrapper);
        if (mSignal != null) {
            DSignalH dSignalH = new DSignalH();
            dSignalH.setDeviceid(mSignal.getDeviceid());
            dSignalH.setScid(FocConcat.SCID);
            dSignalH.setSiteid(mSignal.getSiteid());
            dSignalH.setDeviceid(mSignal.getDeviceid());
            dSignalH.setType(mSignal.getType());
            dSignalH.setId(mSignal.getId());
            dSignalH.setValue(TypeUtils.castToFloat(focPointData.getValue()));
            Date date = new Date();
            date.setTime(focPointData.getTimestam());
            dSignalH.setUpdatetime(date);
           // log.info("插入性能数据:"+ JSONObject.toJSONString(dSignalH));
            dSignalHMapper.insert(dSignalH);
            //更新这条信号的最新数据
            mSignal.setValue(focPointData.getValue());
            mSignal.setUpdatetime(date);
            mSignalService.updateById(mSignal);
        }

    }
}
